package cn.edu.abtu.service.impl;


import cn.edu.abtu.client.UcenterClient;
import cn.edu.abtu.dao.StatisticsDailyDao;
import cn.edu.abtu.entity.StatisticsDaily;
import cn.edu.abtu.service.StatisticsDailyService;
import cn.edu.abtu.vo.ResponseData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


@Service
public class StatisticsDailyServiceImpl implements StatisticsDailyService {

    @Resource
    private StatisticsDailyDao statisticsDailyDao;

    @Resource
    private UcenterClient ucenterClient;


    @Override
    public ResponseData registerCount(String date) {
        QueryWrapper<StatisticsDaily> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("date_calculated",date);
        int delete = statisticsDailyDao.delete(queryWrapper);

        //调用ucentenr服务,统计注册人数
        ResponseData responseData = ucenterClient.countRegister(date);
        Integer registerCount = (Integer) responseData.getData();

        StatisticsDaily statisticsDaily = new StatisticsDaily();
        statisticsDaily.setDateCalculated(date); //统计的日期
        statisticsDaily.setRegisterNum(registerCount); //注册人数
        statisticsDaily.setVideoViewNum(RandomUtils.nextInt(200)); //观看人数
        statisticsDaily.setLoginNum(RandomUtils.nextInt(200)); //登录人数
        statisticsDaily.setCourseNum(RandomUtils.nextInt(10)); //每日新增课程数量
        statisticsDaily.setGmtCreate(new Date());
        statisticsDaily.setGmtModified(new Date());
        int insert = statisticsDailyDao.insert(statisticsDaily);
        if (insert>0){
            return ResponseData.success("生成数据成功",null);
        }
        return ResponseData.fail();
    }

    //图表显示,返回两部分数据,日期JSON数组,数量JSON数组
    @Override
    public ResponseData showData(String type, String begin, String end) {

        //根据条件查询对应数据
        QueryWrapper<StatisticsDaily> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("date_calculated",begin,end);
        queryWrapper.select("date_calculated",type); //查询指定的列
        queryWrapper.orderByAsc("date_calculated");
        List<StatisticsDaily> dailyList = statisticsDailyDao.selectList(queryWrapper);

        List<String> dateCalculatedList = new ArrayList<>();
        List<Integer> numDataList = new ArrayList<>();
        for (StatisticsDaily daily : dailyList) {
            //封装日期List集合
            dateCalculatedList.add(daily.getDateCalculated());
            //封装对应数量
            switch (type){
                case "register_num":
                    numDataList.add(daily.getRegisterNum());
                    break;
                case "login_num":
                    numDataList.add(daily.getLoginNum());
                    break;
                case "video_view_num":
                    numDataList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    numDataList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }
        }
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("dateCalculatedList",dateCalculatedList);
        hashMap.put("numDataList",numDataList);
        return ResponseData.success("查询成功",hashMap);
    }
}
